#include "customchart.h"

double CustomChart::findMaxAbsoluteValue(const QVector<double> &data) {
    if (data.isEmpty()) {
        return 0.0;
    }

    double maxAbsValue = std::abs(data[0]);
    for (double value : data) {
        double absValue = std::abs(value);
        if (absValue > maxAbsValue) {
            maxAbsValue = absValue;
        }
    }

    return maxAbsValue;
}

CustomChart::CustomChart(const QVector<double> &data, QWidget *parent)
    : QChartView(parent)
{
    m_data = data;
    QChart *chart = new QChart();
    //chart->setTheme(QChart::ChartThemeBlueCerulean);
    setChart(chart);
    chart->legend()->hide();

    // 创建折线序列
    m_lineSeries = new QLineSeries();
    for (int i = 0; i < data.size(); ++i)
    {
        m_lineSeries->append(i+1, data[i]);
    }
    chart->addSeries(m_lineSeries);

    // 创建散点序列（实心圆数据点）
    m_scatterSeries = new QScatterSeries();
    for (int i = 0; i < data.size(); ++i)
    {
        m_scatterSeries->append(i+1, data[i]);
        m_scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeCircle);
        m_scatterSeries->setMarkerSize(10.0);
    }
    chart->addSeries(m_scatterSeries);

    // 创建 X 轴
    m_axisX = new QValueAxis();

    int dataWidth = data.size()+1;

    m_axisX->setRange(0, dataWidth);

    m_axisX->setTickCount(dataWidth+1);
    for (int i = 0; i <= dataWidth+1; ++i)
    {
        m_axisX->setTickAnchor(i);
    }
    m_axisX->setLabelFormat("%d"); //设置刻度的格式

    chart->addAxis(m_axisX, Qt::AlignBottom);
    m_lineSeries->attachAxis(m_axisX);
    m_scatterSeries->attachAxis(m_axisX);

    // 创建 Y 轴
    m_axisY = new QValueAxis();

    int max = findMaxAbsoluteValue(data);
    m_axisY->setRange(-max-1, max+1);

    m_axisY->setTickCount(9);
    for (int i = -4; i <= 4; ++i)
    {
        m_axisY->setTickAnchor(i);
    }
    m_axisY->setLabelFormat("%d"); //设置刻度的格式

    chart->addAxis(m_axisY, Qt::AlignLeft);
    m_lineSeries->attachAxis(m_axisY);
    m_scatterSeries->attachAxis(m_axisY);
}

void CustomChart::setData(const QVector<double> &data)
{
    m_lineSeries->clear();
    m_scatterSeries->clear();
    for (int i = 0; i < data.size(); ++i)
    {
        m_lineSeries->append(i+1, data[i]);
    }
    for (int i = 0; i < data.size(); ++i)
    {
        m_scatterSeries->append(i+1, data[i]);
        m_scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeCircle);
        m_scatterSeries->setMarkerSize(10.0);
    }

    int dataWidth = data.size()+1;

    m_axisX->setRange(0, dataWidth);

    m_axisX->setTickCount(dataWidth+1);
    for (int i = 0; i <= dataWidth+1; ++i)
    {
        m_axisX->setTickAnchor(i);
    }
    m_axisX->setLabelFormat("%d"); //设置刻度的格式

    m_lineSeries->attachAxis(m_axisX);
    m_scatterSeries->attachAxis(m_axisX);


    int max = findMaxAbsoluteValue(data);
    m_axisY->setRange(-max-1, max+1);

    m_axisY->setTickCount(9);
    for (int i = -4; i <= 4; ++i)
    {
        m_axisY->setTickAnchor(i);
    }
    m_axisY->setLabelFormat("%d"); //设置刻度的格式
}
